// 1.导入 redux
const redux = require('redux')

const initialState = {
  counter: 0
}

// reducer
function reducer(state = initialState, action) {
  switch (action.type) {
    case 'INCREMENT':
      return { ...state, counter: state.counter + 1 }
      break
    case 'DECREMENT':
      return { ...state, counter: state.counter - 1 }
      break
    case 'ADD_NUMBER':
      return { ...state, counter: state.counter + action.num }
      break
    case 'SUB_NUMBER':
      return { ...state, counter: state.counter - action.num }
      break
    default:
      return state
  }
}

// store
const store = redux.createStore(reducer)

// 订阅store的修改
store.subscribe(() => {
  console.log('counter: ', store.getState().counter)
})

// action
const action1 = { type: 'INCREMENT' }
const action2 = { type: 'DECREMENT' }

const action3 = { type: 'ADD_NUMBER', num: 5 }
const action4 = { type: 'SUB_NUMBER', num: 12 }

// 派发action
store.dispatch(action1)
store.dispatch(action2)
store.dispatch(action2)
store.dispatch(action3)
store.dispatch(action4)